#include "page.h"
#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#include "page4.h"
#include "esp_log.h"
LV_FONT_DECLARE(snow);
LV_FONT_DECLARE(snow16);
LV_FONT_DECLARE(clock_ui);
#define ANIEND  while(lv_anim_count_running())lv_task_handler();//等待动画完成
#define FONTSNOW1 "\xEE\x9F\x8E"
#define FONTSNOW2 "\xEE\x98\x92"
#define FONTSNOW3 "\xEE\x9A\x9C"
#define FONTSNOW4 "\xEE\x9A\x9D"
extern lv_obj_t* scr;
extern lv_obj_t* scr_body;
struct {
    lv_obj_t * label_hh;
    lv_obj_t * label_hl;
    lv_obj_t * label_mh;
    lv_obj_t * label_ml;
    lv_obj_t * label_dian;
    lv_obj_t * cont;
}clock_ui_t;
#define MAXNUM 30
extern struct _blbl_sys_t blbl_sys_t;
struct {
    lv_obj_t * ico_snow;
    uint8_t opa;
    uint8_t x;
    int16_t y;
    uint8_t vx;
    uint8_t vy;
    int16_t state;
}snow_ui[MAXNUM];
void snow_init()
{
    for(uint8_t i=0;i<MAXNUM;i++)
    {
        snow_ui[i].ico_snow=lv_label_create(scr_body,NULL);
        lv_obj_set_style_local_text_font(snow_ui[i].ico_snow,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&snow16);
        lv_obj_set_style_local_text_color(snow_ui[i].ico_snow,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_WHITE);
        lv_label_set_text(snow_ui[i].ico_snow, FONTSNOW1);

        snow_ui[i].y=rand()%(-100)-100;
        snow_ui[i].x=rand()%240;
        snow_ui[i].vy=rand()%3+1;
        snow_ui[i].state=0;
        snow_ui[i].opa=rand()%155+100;
    }
}
void snow_xy_renovate()
{
    for(uint8_t i=0;i<MAXNUM;i++)
    {

        if(snow_ui[i].y>120)
        {
            snow_ui[i].state--;
            if(snow_ui[i].state<1)
            {
                snow_ui[i].y=rand()%(-10)-10;
                snow_ui[i].opa=rand()%155+100;
            }
        }else
        {
            snow_ui[i].state++;
            snow_ui[i].y+=snow_ui[i].vy;
        }
            lv_obj_set_style_local_text_opa(snow_ui[i].ico_snow,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,snow_ui[i].opa);
            lv_obj_set_pos(snow_ui[i].ico_snow,snow_ui[i].x,snow_ui[i].y);
    }
}

void clock_init()
{
    lv_obj_set_size(scr_body,240,135);
    lv_obj_set_pos(scr_body,0,0);

    clock_ui_t.cont=lv_cont_create(scr_body,NULL);
    lv_obj_set_size(clock_ui_t.cont,200,80);
    lv_obj_set_pos(clock_ui_t.cont,20,20);

        static lv_style_t style1;
    lv_style_set_bg_color(&style1, LV_STATE_DEFAULT, LV_COLOR_BLACK);//设置屏幕背景
        lv_style_set_border_color(&style1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_add_style(clock_ui_t.cont, LV_BTN_PART_MAIN, &style1);      /*Default button style*/

    clock_ui_t.label_hh=lv_label_create(clock_ui_t.cont,NULL);
    lv_obj_set_style_local_text_font(clock_ui_t.label_hh,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&clock_ui);
    lv_obj_set_style_local_text_color(clock_ui_t.label_hh,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_YELLOW);
   // lv_label_set_text(clock_ui_t.label_hh, "1");
    lv_label_set_text_fmt(clock_ui_t.label_hh,"%1d",blbl_sys_t.timeinfo.tm_hour/10);  
    lv_obj_set_pos(clock_ui_t.label_hh,0,5);

    clock_ui_t.label_hl=lv_label_create(clock_ui_t.cont,NULL);
    lv_obj_set_style_local_text_color(clock_ui_t.label_hl,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_YELLOW);
    lv_obj_set_style_local_text_font(clock_ui_t.label_hl,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&clock_ui);
 //   lv_label_set_text(clock_ui_t.label_hl, "6");
    lv_label_set_text_fmt(clock_ui_t.label_hl,"%1d",blbl_sys_t.timeinfo.tm_hour%10); 
    lv_obj_set_pos(clock_ui_t.label_hl,40,5);

    clock_ui_t.label_mh=lv_label_create(clock_ui_t.cont,NULL);
    lv_obj_set_style_local_text_color(clock_ui_t.label_mh,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_YELLOW);
    lv_obj_set_style_local_text_font(clock_ui_t.label_mh,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&clock_ui);
  //  lv_label_set_text(clock_ui_t.label_mh, "5");
    lv_label_set_text_fmt(clock_ui_t.label_mh,"%1d",blbl_sys_t.timeinfo.tm_min/10); 
    lv_obj_set_pos(clock_ui_t.label_mh,110,5);

    clock_ui_t.label_ml=lv_label_create(clock_ui_t.cont,NULL);
    lv_obj_set_style_local_text_color(clock_ui_t.label_ml,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_YELLOW);
    lv_obj_set_style_local_text_font(clock_ui_t.label_ml,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&clock_ui);
//    lv_label_set_text(clock_ui_t.label_hl, "0");
    lv_label_set_text_fmt(clock_ui_t.label_ml,"%1d",blbl_sys_t.timeinfo.tm_min%10); 
    lv_obj_set_pos(clock_ui_t.label_ml,150,5);

    clock_ui_t.label_dian=lv_label_create(clock_ui_t.cont,NULL);
    lv_obj_set_style_local_text_color(clock_ui_t.label_dian,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_YELLOW);
    lv_obj_set_style_local_text_font(clock_ui_t.label_dian,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&clock_ui);
    lv_label_set_text(clock_ui_t.label_dian, ":");
    lv_obj_set_pos(clock_ui_t.label_dian,88,0);
}
compment_t c_clock;
void snow_load()
{
    snow_init();
    c_clock.start=135;
    c_clock.end=20;
    c_clock.obj=clock_ui_t.cont;
    c_clock.start_t=600;
    c_clock.end_t=600;
    c_clock.cb=(void *)lv_anim_path_ease_out;
    c_clock.funcb=(void *)lv_obj_set_y;
    c_clock.next=NULL;
}
void page4_load()
{
    clock_init();
    snow_load();
    obj_move(&c_clock,1);
}
lv_task_t * task_time;
void my_task_clock()
{
    static uint8_t i=0,clock_hh,clock_hl,clock_mh,clock_ml;
    uint8_t h,m;
    h=(uint8_t)blbl_sys_t.timeinfo.tm_hour;
    m=(uint8_t)blbl_sys_t.timeinfo.tm_min;
    if(clock_hh!=(h/10))
    {
        clock_hh=h/10;
        lv_label_set_text_fmt(clock_ui_t.label_hh,"%1d",clock_hh);     
    }
    if(clock_hl!=(h%10))
    {
        clock_hl=h%10;
        lv_label_set_text_fmt(clock_ui_t.label_hl,"%1d",clock_hl);         
    }
    if(clock_mh!=(m/10))
    {
        clock_mh=m/10;
        lv_label_set_text_fmt(clock_ui_t.label_mh,"%1d",clock_mh);        
    }
    if(clock_ml!=(m%10))
    {
        clock_ml=m%10;
        lv_label_set_text_fmt(clock_ui_t.label_ml,"%1d",clock_ml);           
    }
    snow_xy_renovate();
    if(i%10==2)
        lv_obj_set_hidden(clock_ui_t.label_dian,1);
    else if(i%10==8)
        lv_obj_set_hidden(clock_ui_t.label_dian,0);
    i++;
    if(i==250)
        i=0;
}
void page4_end()
{
    lv_task_del(task_time);
    for(uint8_t i=0;i<MAXNUM;i++)
    {
        lv_obj_del(snow_ui[i].ico_snow);
    }
    obj_move(&c_clock,0);
    ANIEND
    lv_obj_clean(scr_body);//删除scr_body
    lv_obj_set_size(scr_body,240,135-16);
    lv_obj_set_pos(scr_body,0,16);
}
void page4_start()
{
    page4_load();
    task_time = lv_task_create(my_task_clock, 100, LV_TASK_PRIO_MID, NULL);
}
